

/* define the MainMenu class (attatched to the main menu DOM element) */
function MainMenu(containerRef, slidingPagesRef)
{
	this.container = $(containerRef);
	this.slidingPages = slidingPagesRef;
	this.init();
}

MainMenu.prototype.init= function()
{
	var self= this;
	self.numItems = (self.container).find("ul li").length;
	self.selectPageIndex(1);
	
	// go next
	this.container.on("click","#next", function(ev)
	{
		ev.preventDefault();
		if(self.currentSelected < self.numItems)
		{
			if(self.slidingPages.validatePage(self.currentSelected-1))//it's 0 based
			{
				self.selectPageIndex(self.currentSelected +1);
			}
		}
	});
	
	// go prev
	this.container.on("click","#prev", function(ev)
	{
		ev.preventDefault();
		if(self.currentSelected > 1)
		{
			self.selectPageIndex(self.currentSelected - 1);
		}
	});
}

MainMenu.prototype.selectPageIndex = function(index)
{
	var self = this;
	self.currentSelected=index;
	$(self.container).find("#pageCountContainer li.current").removeClass("current")
	$(self.container).find("#pageCountContainer li:nth-child("+index+")").addClass("current");
	self.slidingPages.showPage(index-1); //it's 0 based
}


MainMenu.prototype.submit= function(url, success_callbackFunction, fail_callbackFunction)
{
	var self= this;

	var submitionObj = {};

		//parse the DOM for complient elements and get their value:
	$("*").filter(function(){return $(this).data('store') !== undefined;})
			.each(function(key, value)
				{
					var tagType = $(value).attr("type");
				
					if(tagType =="checkbox")
					{
						submitionObj[$(value).data("store")] = $(value).is(":checked"); 
					}
					else if(tagType =="radio")
					{
						//find all siblings of this radio and store only the one checked:
						var checkedRadio = $("input[name="+$(value).attr("name")+"]:checked");
						
						if(checkedRadio.size()>0)
						{
							submitionObj[checkedRadio.data("store")] = checkedRadio.val();
						}else
						{
							 submitionObj[$(value).data("store")] = "undefined";
						}
					}
					else
					{
						submitionObj[$(value).data("store")] = $(value).val();
					}
				});

	//call the storing script:			
	var jqxhr = $.post( url,submitionObj)
			  .done(function(responce) {
				success_callbackFunction(responce);
			  })
			  .fail(function() {
				fail_callbackFunction();
			  });
			 
}


/*the content container class - takes care of loading content */

function SlidingPages(containerRef)
{
	this.container = containerRef;
	this.init();
	
}

SlidingPages.prototype.init =function()
{
	var self=this;
	//cash the page references:
	self.pageRefsArr = $(self.container).find("section.page").toArray();

	//distribute all pages
	for(var i=0 ; i< self.pageRefsArr.length; i++)
	{
			var currentOffsetPerc= i* 100 + "%";
			$(self.pageRefsArr[i]).css("left", currentOffsetPerc);
			
			//if validation requested then enable it:
			$(self.pageRefsArr[i]).find(".validationEnabled").validationEngine();
	}
	
}

SlidingPages.prototype.validatePage = function(n)
{
	var self=this;
	
	var validatingForm = $(self.pageRefsArr[n]).find("form.validationEnabled");
	if(validatingForm.length >0)
	{
		if(!validatingForm.validationEngine('validate'))
		{
			$("#validationErrorModal").modal();
			return false;
		}	
	}
	return true;
}

SlidingPages.prototype.showPage= function(n)
{
	var self=this;
//if is not the current page
	if(!$(self.pageRefsArr[n]).hasClass("current"))
	{
	
		for(var i=0; i< self.pageRefsArr.length; i++)
		{
			var currentOffsetPerc= (i - n) * 100 + "%";
			$(self.pageRefsArr[i]).removeClass("current");
			$(self.pageRefsArr[i]).clearQueue();
			$(self.pageRefsArr[i]).animate({left: currentOffsetPerc }, 900, "easeInOutExpo");
		}
	}
}



